package com.fanbai.score.user.activity;

import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.view.divider.DividerItemDecoration;
import com.fanbai.score.user.bean.DQRJF_Bean;
import com.fanbai.score.user.http.UserOrder_RequestNet_Utils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.R.attr.data;

/**
 * Created by acer on 2017/6/10.
 */

public class DaiQueRenJiFen_Activity extends BaseActivity{
    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;

    @Override
    protected int getLayoutID() {
        return R.layout.user_activity_dqrjf;
    }

    @Override
    public String getTitleName() {
        return "待确认积分";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        requestUnConfirm();
    }

    private void setData(List<DQRJF_Bean> data){
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(ctx, DividerItemDecoration.VERTICAL_LIST));
        CommonAdapter adapter = new CommonAdapter<DQRJF_Bean>(ctx, R.layout.user_item_activity_dqrjf, data) {
            @Override
            protected void convert(ViewHolder holder, final DQRJF_Bean bean, int position) {
                holder.setText(R.id.itenExpandableListView_groupName, bean.getSinfo().getSname());
                TextView okBut = holder.getView(R.id.itenExpandableListView_childOkBut);
                TextView groupFlag = holder.getView(R.id.itenExpandableListView_groupFlag);
                if(bean.getOstate().equals("10")){
                    okBut.setVisibility(View.VISIBLE);
                    groupFlag.setText("您的积分已激活");
                }else{
                    okBut.setVisibility(View.GONE);
                    groupFlag.setText("积分正在审核");
                }
                holder.setText(R.id.itenExpandableListView_childTotal, "总价："+bean.getOtotalprice());
                holder.setText(R.id.itenExpandableListView_childJiFen, "积分："+bean.getOscore());
                holder.setText(R.id.itenExpandableListView_childTime, ""+bean.getSinfo().getSupdatetime());
                holder.setOnClickListener(R.id.itenExpandableListView_childOkBut, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        confirmOrderScore(bean.getOid());
                    }
                });
            }
        };
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }


    private void requestUnConfirm(){
        ctx.showDialog();
        UserOrder_RequestNet_Utils.getUnConfirmOrderInfos(ctx, new CallBack<List<DQRJF_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<DQRJF_Bean> beans) {
                ctx.hideDialog();
                setData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void confirmOrderScore(String oid){
        ctx.showDialog();
        UserOrder_RequestNet_Utils.confirmOrderScore(ctx, oid, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ctx.hideDialog();
                ToastUtils.RightImageToast(ctx, s);
                requestUnConfirm();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
